'use strict';

let DaoBase = require('./DaoBase');
let db = require('./models');
let D = require('./function');
let F = require('../../function');

var util = require("util");

function OrderDao(data) {
  data = data || {};
  data.model = data.model || db.order;
  DaoBase.call(this, data);
}

util.inherits(OrderDao, DaoBase);

/*
 *@todo 查询
 *@param  {Object}  query  查询的内容
 *@param  {Object}  opt    其他参数
 */

OrderDao.prototype.getByQuery = function*(query, page, pageSize) {
  page = page || 1;
  pageSize = pageSize || 50;
  let offset = (page - 1) * pageSize;
  let ret = yield  this.model.findAll({where: query, limit: pageSize, offset: offset});
  if (!ret) return F.returnData(1, '查询失败');
  return F.returnData(0, '查询成功.', ret)
};


/*
 @todo 查询一条 并保存
 @query 查询条件
 @opt 参数
 */
OrderDao.prototype.findAndSave = function* (doc, opt) {
  let ret = this.getOneByQuery({orderid: doc.orderid});
  if (ret.error == 1) return F.returnData(1, ret.msg, ret.data);
  //let result = yield this.save(doc);
  return F.returnData(0, result.msg, result.data);
};


module.exports = OrderDao;


/*

 var Model = require('./models/order.js');




 function DaoMysql() {
 }
 module.exports = new DaoMysql();

 DaoMysql.prototype.save = function*(json) {
 yield Model.sync();// create table success

 let instance = Model.build(json);
 let result = yield instance.save();

 return result;
 };

 DaoMysql.prototype.findById = function*(id) {
 return yield Model.findById(id);
 };
 */